package com.yunhe.sms.service.system.impl;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.mail.Mail;
import com.yunhe.commons.util.mail.MailUtils;
import com.yunhe.sms.dao.system.UserDao;
import com.yunhe.sms.dao.system.impl.UserDaoImpl;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.system.RoleService;
import com.yunhe.sms.service.system.UserService;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/16 0016.
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private RoleService roleService = new RoleServiceImpl();
    /**
     * 增
     * @param user
     * @throws SQLException
     */
    public void save(User user){
        userDao.save(user);
    }

    /**
     * 删
     * @param id
     * @throws SQLException
     */
    public void delete(Long id){
        userDao.delete(id);
    }

    @Override
    public void delBatch(String ids) {
        if(null!=ids){
            String[] idArr = ids.split(",");
            for(String i:idArr){
                userDao.delete(Long.valueOf(i));
            }
        }
    }

    /**
     * 改
     * @param user
     * @throws SQLException
     */
    public void update(User user){
        userDao.update(user);
    }

    @Override
    public void updatePwd(String password, Long id) {
        userDao.updatePwd(password,id);
    }

    @Override
    public User findOne(String userName, String password) {
        User user = userDao.findOne(userName, password);
        return user;
    }


    public Boolean findOne(String userName){
        User user = userDao.findOne(userName);

        Boolean flag = false;
        if(null != user){
            flag = true;
        }
        return flag;
    }

    public User findUser(String userName){
        User user = userDao.findOne(userName);
        return user;
    }

    public  User findOne(Long id){
        User user = userDao.findOne(id);
        return user;
    }
    public  List<User> findList(){
        List<User> userList = userDao.findList();
        return userList;
    }

    @Override
    public Pageable<User> findPage(Integer offset, Integer limit) {
        Pageable<User> pageable = userDao.findPage(offset, limit);
        /*List<User> users = pageable.getRows();
        for(User user : users){
            Role role = roleService.find(user.getRoleId());
            if(role.getStatus() == 0){
                user.setRoleName(null);
            }
        }
        pageable.setRows(users);*/
        return pageable;
    }

    @Override
    public String sendEmail(User user) {

        String msg = "邮件发送失败";
        if(null != user){
            String url = "http://localhost:8080/login";
            String contents = "已为您添加学员信息管理系统用户账号，用户名："+user.getUserName()+"，密码："+user.getPassword()+"，请及时登录账号修改密码。<br><a href=\""+url+"\">点击此链接登录账号</a>";
            Mail mail = new Mail("2603459843@qq.com", user.getMail(), "账号添加通知", contents);

            try {
                MailUtils.mail(mail);
                msg = "邮件发送成功";
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return msg;
    }
}
